﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using Stiline.DAL.Entities;
//using Stiline.DAL.Repositories;
using AdoDAL.Entities;
using AdoDAL.Repositories;

using Stiline.Web.Models.Task;
using System.Web.Security;

namespace Stiline.Web.Controllers
{
    public class TaskController : Controller
    {
        //
        // GET: /Task/

        public ActionResult Get(int taskId)
        {
            var taskRepository = new TaskRepository();
            var task = taskRepository.Get(taskId);
            if (task == null)
            {
                ViewBag.Error = true;
                ViewBag.ErrorMessage = "No such task";
            }
            return View("TaskInfo", task);
        }

        public ActionResult Index()
        {
            var taskRepository = new TaskRepository();
            var user = Membership.GetUser() as User;
            if (user != null)
            {
                var tasks = taskRepository.GetAll().Where(t => t.UserId.Equals(user.Id));
                return View("TaskList", tasks);
            }
            return RedirectToAction("Index", "Home");

        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(TaskModel taskModel)
        {
            if (ModelState.IsValid)
            {
                var taskRepository = new TaskRepository();
                var user = Membership.GetUser() as User;
                if (user == null)
                {
                    return View();
                }
                var newTask = new Task
                    {
                        Title = taskModel.Title,
                        Description = taskModel.Description,
                        StartTime = taskModel.Start,
                        EndTime = taskModel.End,
                        UserId = user.Id
                    };
                taskRepository.Save(newTask);
                return RedirectToAction("Index");
            }
            return View();
        }

        [Authorize]
        public ActionResult Delete(int id)
        {
            var taskRepository = new TaskRepository();
            var task = taskRepository.Get(id);
            var user = Membership.GetUser() as User;
            if (user == null)
            {
                return RedirectToAction("Index", "Home");
            }
            if (task != null)
            {
                if (task.UserId.Equals(user.Id))
                {
                    taskRepository.Delete(task.Id);
                }
            }
            return RedirectToAction("Index", "Task");
        }

        [Authorize]
        public ActionResult Calendar(int? id)
        {
            var currentUser = Membership.GetUser() as User;
            if (!id.HasValue)
            {
                ViewBag.userId = currentUser.Id;
                return View();
            }
            if (currentUser != null && currentUser.Id.Equals(id.Value))
            {
                ViewBag.calendarReadonly = false;
            }
            else
            {
                ViewBag.calendarReadonly = true;
            }
            ViewBag.userId = id;
            return View();
        }
    }
}
